package cn.wolfcode.p2p.controller;

import cn.wolfcode.p2p.base.anno.NeedLogin;
import cn.wolfcode.p2p.base.domain.LoginInfo;
import cn.wolfcode.p2p.base.domain.OrderTime;
import cn.wolfcode.p2p.base.domain.UserInfo;
import cn.wolfcode.p2p.base.domain.VideoAuth;
import cn.wolfcode.p2p.base.exception.CustomerException;
import cn.wolfcode.p2p.base.service.ILoginInfoService;
import cn.wolfcode.p2p.base.service.IOrderTimeService;
import cn.wolfcode.p2p.base.service.IUserInfoService;
import cn.wolfcode.p2p.base.service.IVideoAuthServie;
import cn.wolfcode.p2p.util.JsonResult;
import cn.wolfcode.p2p.util.UserContext;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
public class VedioAuthController {
    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private IVideoAuthServie videoAuthServie;
    @Autowired
    private ILoginInfoService loginInfoService;
    @Autowired
    private IOrderTimeService orderTimeService;

    @RequestMapping("videoAuditOrder")
    @NeedLogin
    public String videoAuditOrder(Model model) {
        UserInfo userInfo = userInfoService.getById(UserContext.getCurrentUser().getId());
        if (userInfo.hasVedioAuth()) {
            //通过了视频认证-->通过页面
            model.addAttribute("videoSuccess", true);
        } else if (userInfo.getVideAuthId() != null) {
            //申请了,但还没有通过 -->预约页面
            VideoAuth videoAuth = videoAuthServie.getById(userInfo.getVideAuthId());
            model.addAttribute("videoAuditOrder", videoAuth);
        } else {
            //还没有申请,正常申请页面
            //查询出所有的客服
            List<LoginInfo> auditors = loginInfoService.listAuditors();
            model.addAttribute("auditors", auditors);
            //创建预约日期, 默认3天
            ArrayList<Date> orderDates = new ArrayList<>();
            Date date = new Date();
            orderDates.add(DateUtils.addDays(date, 1));
            orderDates.add(DateUtils.addDays(date, 2));
            orderDates.add(DateUtils.addDays(date, 3));
            model.addAttribute("orderDates", orderDates);
            //预约时间段表
            List<OrderTime> orderTimes = orderTimeService.listAll();
            model.addAttribute("orderTimes", orderTimes);
        }
        return "borrow/videoOrder";
    }

    @RequestMapping("saveVideoAuditOrder")
    @ResponseBody
    @NeedLogin
    public JsonResult saveVideoAuditOrder(VideoAuth videoAuth) {
        JsonResult jsonResult = new JsonResult();
        try {
            videoAuthServie.apply(videoAuth);
        } catch (CustomerException e) {
            jsonResult.setMessage(e.getMessage());
            throw new CustomerException(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("系统繁忙中,请稍后再试");
        }
        return jsonResult;
    }
}
